﻿@model List<ShoppingCart>

@{
    var stringHelper = new StringHelper();
    var imageHelper = new ImageHelper();
}

<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h5 class="modal-title" id="shoppingCartModalLabel">
                <i class="fa fa-shopping-cart"></i> Giỏ hàng
            </h5>
        </div>
        @if (Model.Any())
        {
            float totalPrice = 0;
            float exchangeRate = 20000;

            <div class="modal-body no-padding">
                <table class="table table-bordered no-margin">
                    @foreach (ShoppingCart item in Model)
                    {
                        Product product = item.Style.Product;
                        Style style = item.Style;
                        Property color = style.StyleProperties
                            .Select(x => x.Property)
                            .FirstOrDefault(x => x.PropertyType.NameId == "color");
                        float priceQuantity = style.UnitPrice * item.Quantity;
                        totalPrice += priceQuantity;

                        Image image = new Image();
                        if (color != null)
                        {
                            image = product.Images
                                .Where(x => x.PropertyId == color.Id)
                                .OrderByDescending(x => x.Priority)
                                .FirstOrDefault();
                        }
                        else
                        {
                            image = product.Images
                                .OrderByDescending(x => x.Priority)
                                .FirstOrDefault();
                        }

                        string imageUrl = image.Url + "?w=100";
                        string imagePadding = "0";

                        if (!imageHelper.Standard(image.Width, image.Height))
                        {
                            imageUrl = image.Url + "?w=100&h=100";
                            imagePadding = "17% 0";
                        }

                        <tr>
                            <td class="image">
                                <img class="img-responsive center-block" src="@imageUrl" style="padding: @imagePadding" />
                            </td>
                            <td>
                                <p class="name">
                                    @product.Name
                                </p>
                                <p class="code">
                                    @product.Code
                                </p>
                                <p class="type">
                                    @foreach (Property item2 in style.StyleProperties.Select(x => x.Property).OrderBy(x => x.Name))
                                    {
                                        <span>@item2.PropertyType.Name: <b>@item2.Name</b></span>
                                    }
                                </p>
                                <p class="visible-xs">
                                    <b style="font-size: 16px;">@stringHelper.PriceFormat(priceQuantity, "usd")</b> <span style="color: #777; font-size: 13px;">@stringHelper.PriceFormat(priceQuantity * exchangeRate, "vnd")</span>
                                </p>
                                <p class="visible-xs">
                                    <select data-id="@style.Id" onchange="new ShoppingCart().Edit(this)">
                                        @for (int i = 1; i <= 10; i++)
                                        {
                                            string selected = (i == item.Quantity) ? "selected" : "";

                                            <option value="@i" @selected>@i</option>
                                        }
                                    </select>
                                </p>
                            </td>
                            <td class="quantity hidden-xs">
                                <select data-id="@style.Id" onchange="new ShoppingCart().Edit(this)">
                                    @for (int i = 1; i <= 10; i++)
                                    {
                                        string selected = (i == item.Quantity) ? "selected" : "";

                                        <option value="@i" @selected>@i</option>
                                    }
                                </select>
                            </td>
                            <td class="hidden-xs">
                                <p class="price">
                                    @stringHelper.PriceFormat(priceQuantity, "usd")
                                </p>
                                <p class="price-vn">
                                    @stringHelper.PriceFormat(priceQuantity * exchangeRate, "vnd")
                                </p>
                            </td>
                            <td class="control">
                                <a data-id="@style.Id" class="btn btn-link delete" onclick="new ShoppingCart().Delete(this)">
                                    <i class="fa fa-times text-danger"></i>
                                </a>
                            </td>
                        </tr>
                    }
                    <tr class="total hidden-xs">
                        <td colspan="3" class="title">
                            Tổng tiền
                        </td>
                        <td colspan="2">
                            <span class="usd">
                                @stringHelper.PriceFormat(totalPrice, "usd")
                            </span>
                            <span class="vnd">
                                @stringHelper.PriceFormat(totalPrice * exchangeRate, "vnd")
                            </span>
                        </td>
                    </tr>
                    <tr class="hidden-xs">
                        <td colspan="5" class="warning text-warning">
                            <i>- Thời gian giao hàng dự kiến từ 5 tới 10 ngày làm việc (trừ T7 &amp; CN).</i>
                            <br />
                            <i>- Miễn phí vận chuyển toàn quốc.</i>
                        </td>
                    </tr>
                    <tr class="total visible-xs">
                        <td class="title">
                            Tổng tiền
                        </td>
                        <td colspan="2">
                            <span class="usd">
                                @stringHelper.PriceFormat(totalPrice, "usd")
                            </span>
                            <span class="vnd">
                                @stringHelper.PriceFormat(totalPrice * exchangeRate, "vnd")
                            </span>
                        </td>
                    </tr>
                    <tr class="visible-xs">
                        <td colspan="3" class="warning text-warning">
                            <i>- Thời gian giao hàng dự kiến từ 5 tới 10 ngày làm việc (trừ T7 &amp; CN).</i>
                            <br />
                            <i>- Miễn phí vận chuyển toàn quốc.</i>
                        </td>
                    </tr>
                </table>

            </div>
            <div class="modal-footer">
                <a href="@Url.Action("Index", "Receiver")" class="btn btn-primary">Thanh toán</a>
            </div>
        }
        else
        {
            <div class="modal-body">
                <img src="/img/no-shopping-cart.png?w=300" class="center-block" />
                <h3 class="text-center text-danger">Không có sản phẩm trong giỏ hàng</h3>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Đóng</button>
            </div>
        }
    </div>
</div>